﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.SystemNews
{
    public partial class EditSystemNews :BasePage
    {
        NJL.BLL.NJL_SYS_CS_NEWS_TYPE bllNewsType = new BLL.NJL_SYS_CS_NEWS_TYPE();
        NJL.BLL.NJL_SYS_NEWS bll = new BLL.NJL_SYS_NEWS();
        NJL.Model.NJL_SYS_NEWS model;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = bllNewsType.GetList("");

                dllNewsType.DataSource = ds;
                dllNewsType.DataTextField = "TYPE_NAME";
                dllNewsType.DataValueField = "TYPE_CODE";
                dllNewsType.DataBind();
                dllNewsType.Items.Insert(0, new ListItem("请选择", "0"));
                model = bll.GetModel(Request.QueryString["id"].ToString());
                txtTitle.Text = model.NEWS_TITLE;
                txtSubtitle.Text = model.NEWS_SUBJECT_TITLE;
                dllNewsType.Items.FindByValue(model.TYPE_CODE).Selected = true;
                txtTag.Text = model.NEWS_TAG;
                if (model.IS_LINK_TO == 1)
                {
                    rblIsTurn.SelectedIndex = 0;

                }
                else
                {
                    rblIsTurn.SelectedIndex = 1;
                }
                txtTurnUrl.Text = model.LINK_TO_URL;
                txtContent.Text = model.NEWS_CONTECT;
                txtSeoTitle.Text = model.SEO_TITLE;
                txtSeoKeyworkds.Text = model.SEO_KEYWORDS;
                txtSeoDescription.Text = model.SEO_DESCRIPTION;
                txtSort.Text = model.SORT.ToString();
                if (model.IMAGE_CODE != "")
                {
                    Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                }
                else
                {
                    Image1.Visible = false; 
                }
            }        
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(Request.QueryString["id"].ToString());
                DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                model.IMAGE_CODE = SaveUploadFile(Request.Files);
                if(bll.Update(model))
                {
                    Royalsoft.Common.MessageBox.Show(this,"上传成功");
                    Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                    Image1.Visible = true;
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model = bll.GetModel(Request.QueryString["id"].ToString());
            model.NEWS_TITLE = txtTitle.Text.Trim();
            model.NEWS_SUBJECT_TITLE = txtSubtitle.Text.Trim();
            model.TYPE_CODE = dllNewsType.SelectedValue;
            model.NEWS_TAG = txtTag.Text.Trim();
            model.IS_LINK_TO = Convert.ToInt32(rblIsTurn.SelectedValue);                    //1转向 0 不转向
            model.LINK_TO_URL = txtTurnUrl.Text.Trim();
            model.NEWS_CONTECT = txtContent.Text.Trim();
            model.SEO_TITLE = txtSeoTitle.Text.Trim();
            model.SEO_KEYWORDS = txtSeoKeyworkds.Text.Trim();
            model.SEO_DESCRIPTION = txtSeoDescription.Text.Trim();
            model.NJL_SYS_BASE_ID = WEBSITE_CODE;
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.UPDATE_USER_ID = USER_ID;
            model.UPDATE_USER_NAME = USER_NAME;
            model.SORT = int.Parse(txtSort.Text.Trim());
            if (bll.Update(model))
            {
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "6";  //新闻6
                            modelTag.USED_COUNT = 0;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "EditSystemNews.aspx?id="+Request.QueryString["id"]);
            }
        }
    }
}